Sheharyar BSCS-A1


Below is the Code for the Calculator
Copy button will be fixed soon

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>

body{
background-color: coral;
}

button {
font-size: 35;
background-color: black;
border: 0px;
color: white;
width: 130px;
height: 50px;
}

button:hover {
background-color: rgb(66, 66, 66);
}
button:active{
background-color: rgb(196, 157, 85);
}

.opbutton {
background-color: rgb(51, 51, 51);
}

#inputfield {
width: 517px;
height: 35px;
border: none;
outline: none;
font-size: 32px;
}

#cal_container {
min-width: max-content;
height: min-content;
margin: auto;
margin-top: 10%;
font-size: 0;
text-align: center;
}
</style>

</head>

<body>
<div id="cal_container">
<input id="inputfield" type="text" readonly><br>
<button onclick="show(1);">1</button>
<button onclick="show(2);">2</button>
<button onclick="show(3);">3</button>
<button onclick="reslt();" class="opbutton" >=</button><br>
<button onclick="show(4);">4</button>
<button onclick="show(5);">5</button>
<button onclick="show(6);">6</button>
<button onclick="add();" class="opbutton">+</button><br>
<button onclick="show(7);">7</button>
<button onclick="show(8);">8</button>
<button onclick="show(9);">9</button>
<button onclick="subt();" class="opbutton">-</button><br>
<button class="opbutton" onclick="clr();">CE</button>
<button onclick="show(0);">0</button>
<button onclick="mult();" class="opbutton">*</button>
<button onclick="dvde();" class="opbutton">/</button>
</div>

<marquee scrollamount="3">Sheharyar BSCS-A1</marquee>
 

 

</body>


<script>
//after getting answer from operation(+-/*) then inputting values result in extending the same result string
// i.e: 13+2 => =
// 15 => 3
// 153
//so we used a variable check if answer is showed if yes it is showed then the above last step will be
// 15 => 3 => 3
// the above statment will become 3 instead of increasing string like before

var answershowd = 0;

// to clear screen
function clr() {
//this will search for element in whole document with if "inputfield" and gets its value then chge it
document.getElementById("inputfield").value = "";
}

//when we put some number (1,2...0)
function show(x) {
if (answershowd == 0) {
document.getElementById("inputfield").value += x;
}

// if answer is already showed then we dont want to extend the same n0 instead put whole new no
else if (answershowd == 1) {
document.getElementById("inputfield").value = x;
//after putting first no it will required to put next values with in the same no like string
answershowd = 0;
}
}


// (variable)answershowed = 0 is used in all the operation(/*-+)
// so that it wont remove whole no string see line(70-100) for explantion

//funtion for addition
function add() {
document.getElementById("inputfield").value += "+";
answershowd = 0;
}

//funtion for subtraction
function subt() {
document.getElementById("inputfield").value += "-";
answershowd = 0;
}

//function for multiplication
function mult() {
document.getElementById("inputfield").value += "*";
answershowd = 0;
}

//funcion for divide
function dvde() {
document.getElementById("inputfield").value += "/";
answershowd = 0;
}


//function for result
function reslt() {
//this line is responsible for whole calculation a function eval() is used which solves all the
//operation(/*-+) on numbers and give reslt of it
document.getElementById("inputfield").value = eval(document.getElementById("inputfield").value);

// see line(70-100) for more explanation
answershowd = 1;
}
</script>

</html>

In case you wondering how i wrote all the css for the above code snippet I just use a wesite known as hilite.me (and put it in my customize div )which modify your code to make it possible to get it on the web page